// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-06-11 17:49:23
// 生成路径: internal/app/app/logic/app_nav.go
// 生成人：gfast
// desc:应用导航
// company:云南奇讯科技有限公司
// ==========================================================================

package appNav

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/app/dao"
	"github.com/tiger1103/gfast/v3/internal/app/app/model"
	"github.com/tiger1103/gfast/v3/internal/app/app/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/app/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterAppNav(New())
}

func New() service.IAppNav {
	return &sAppNav{}
}

type sAppNav struct{}

func (s *sAppNav) List(ctx context.Context, req *model.AppNavSearchReq) (listRes *model.AppNavSearchRes, err error) {
	listRes = new(model.AppNavSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.AppNav.Ctx(ctx).WithAll()
		if req.Id != "" {
			m = m.Where(dao.AppNav.Columns().Id+" = ?", req.Id)
		}
		if req.Name != "" {
			m = m.Where(dao.AppNav.Columns().Name+" like ?", "%"+req.Name+"%")
		}
		if req.Icon != "" {
			m = m.Where(dao.AppNav.Columns().Icon+" = ?", req.Icon)
		}
		if req.Description != "" {
			m = m.Where(dao.AppNav.Columns().Description+" = ?", req.Description)
		}
		if len(req.DateRange) != 0 {
			m = m.Where(dao.AppNav.Columns().CreatedAt+" >=? AND "+dao.AppNav.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
		}
		if req.Status != "" {
			m = m.Where(dao.AppNav.Columns().Status+" = ?", gconv.Int(req.Status))
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id asc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		var res []*model.AppNavListRes
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
		listRes.List = make([]*model.AppNavListRes, len(res))
		for k, v := range res {
			listRes.List[k] = &model.AppNavListRes{
				Id:          v.Id,
				Name:        v.Name,
				Icon:        v.Icon,
				Description: v.Description,
				CreatedAt:   v.CreatedAt,
				Status:      v.Status,
			}
		}
	})
	return
}

func (s *sAppNav) GetById(ctx context.Context, id uint) (res *model.AppNavInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.AppNav.Ctx(ctx).WithAll().Where(dao.AppNav.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
		if res != nil {
		}
	})
	return
}

func (s *sAppNav) Add(ctx context.Context, req *model.AppNavAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNav.Ctx(ctx).Insert(do.AppNav{
			Name:        req.Name,
			Icon:        req.Icon,
			Description: req.Description,
			Status:      req.Status,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sAppNav) Edit(ctx context.Context, req *model.AppNavEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNav.Ctx(ctx).WherePri(req.Id).Update(do.AppNav{
			Name:        req.Name,
			Icon:        req.Icon,
			Description: req.Description,
			Status:      req.Status,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sAppNav) Delete(ctx context.Context, ids []uint) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNav.Ctx(ctx).Delete(dao.AppNav.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}
